<template>
  <div class="dict-data-page">
    <!-- 搜索表单 -->
    <el-form
      :model="table.where"
      class="ele-form-search ele-table-tool-default"
      size="small"
      @keyup.enter.native="$refs.table.reload()"
      @submit.native.prevent
    >
      <el-row :gutter="15">
        <el-col :md="12" :sm="12">
          <div class="ele-form-actions">
            <el-button
              type="primary"
              icon="el-icon-search"
              class="ele-btn-icon"
              size="small"
              @click="$refs.table.reload()"
            >查询
            </el-button>
            <el-button type="primary" icon="el-icon-plus" class="ele-btn-icon" size="small" @click="showEdit=true">添加
            </el-button>
          </div>
        </el-col>
      </el-row>
    </el-form>
    <!-- 数据表格 -->
    <ele-data-table ref="table" :config="table" :choose.sync="choose" :height="tbHeight" highlight-current-row>
      <el-table-column prop="name" label="策略名称" show-overflow-tooltip min-width="120" />
      <el-table-column prop="key" label="策略编号" show-overflow-tooltip min-width="110" />
      <el-table-column prop="scanner" label="扫描器" width="90px" />
      <el-table-column label="操作" width="130px" align="center" :resizable="false">
        <template slot-scope="{row}">
          <el-link icon="el-icon-edit" type="primary" :underline="false" @click="edit(row)">修改
          </el-link>
          <el-popconfirm title="确定要数据吗？" class="ele-action" @confirm="remove(row)">
            <el-link slot="reference" icon="el-icon-delete" type="danger" :underline="false">删除
            </el-link>
          </el-popconfirm>
        </template>
      </el-table-column>
    </ele-data-table>
    <!-- 编辑弹窗 -->
    <el-dialog
      :title="editForm.dictDataId?'修改扫描策略':'添加扫描策略'"
      :visible.sync="showEdit"
      width="600px"
      :destroy-on-close="true"
      :lock-scroll="false"
      @closed="editForm={}"
    >
      <el-form ref="editForm" :model="editForm" :rules="editRules" label-width="96px">
        <el-form-item label="策略名称:" prop="name">
          <el-input v-model="editForm.name" placeholder="请输入策略名称" clearable />
        </el-form-item>
        <el-form-item label="策略标识:" prop="key">
          <el-input v-model="editForm.key" placeholder="请输入策略标识" clearable />
        </el-form-item>
        <el-form-item label="扫描器类型:" prop="scanner">
          <el-select
            v-model="editForm.scanner"
            placeholder="请选择扫描器类型"
            style="width: 100%"
          >
            <el-option
              v-for="(item, key) in scanner"
              :key="'type' + key"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="备注:">
          <el-input v-model="editForm.description" placeholder="请输入备注" :rows="4" type="textarea" />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="showEdit=false">取消</el-button>
        <el-button type="primary" @click="save">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'SysDictData',
  props: {
    dictId: {
      type: String,
      default: '0'
    },
    tbHeight: {
      type: String,
      default: '10'
    }
  },
  data() {
    return {
      table: { url: `/scanner/list/policies/${this.dictId}/`, where: {}, page: false }, // 表格配置
      choose: [], // 表格选中数据
      showEdit: false, // 是否显示表单弹窗
      editForm: {}, // 表单数据
      editRules: { },
      scanner: []
    }
  },
  watch: {
    /* 监听id变化 */
    dictId() {
      this.table.url = `/scanner/list/policies/${this.dictId}/`
      this.$refs.table.reload()
    }
  },
  mounted() {
    this._scanner()
  },
  methods: {
    /* 显示编辑 */
    edit(row) {
      this.editForm = Object.assign({}, row)
      this.showEdit = true
    },
    /* 保存编辑 */
    save() {
      this.$refs['editForm'].validate((valid) => {
        if (valid) {
          const loading = this.$loading({ lock: true })
          const url = this.editForm.id ? `/scanner/base/update/policies/${this.editForm.id}/` : `/scanner/base/create/policies/${this.dictId}/`
          this.$http.post(url, this.editForm).then(res => {
            loading.close()

            if (res.data.code === 0) {
              this.showEdit = false
              this.$message({ type: 'success', message: res.data.msg })
              this.$refs.table.reload()
            } else {
              this.$message.error(res.data.msg)
            }
          }).catch(e => {
            loading.close()
            this.$message.error(e.message)
          })
        } else {
          return false
        }
      })
    },
    /* 删除 */
    remove(row) {
      const loading = this.$loading({ lock: true })
      this.$http.get(`/scanner/base/delete/policies/${row.id}/`).then(res => {
        loading.close()
        if (res.data.code === 0) {
          this.$message({ type: 'success', message: res.data.msg })
          this.$refs.table.reload()
        } else {
          this.$message.error(res.data.msg)
        }
      }).catch(e => {
        loading.close()
        this.$message.error(e.message)
      })
    },
    /* 获得节点列表 */
    _scanner() {
      this.$http.get('/scanner/select/scanner/').then(res => {
        this.scanner = res.data.data
      })
    }
  }
}
</script>

<style scoped>
</style>
